package com.zyf.springcloud.service.iml;

import com.zyf.springcloud.service.OpenFeignEducationAPI;
import com.zyf.springcloud.service.OpenFeignEducationPermission;
import com.zyf.springcloud.service.OpenFeignEducationRolePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;
import java.util.List;


@Component
public class CustomizeFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {
    AntPathMatcher antPathMatcher = new AntPathMatcher();

    @Autowired
    OpenFeignEducationRolePermission openFeignEducationRolePermission;
    @Autowired
    OpenFeignEducationPermission openFeignEducationPermission;
    @Autowired
    OpenFeignEducationAPI openFeignEducationAPI;
    @Override
    public Collection<ConfigAttribute> getAttributes(Object o) throws IllegalArgumentException {
        //获取请求地址
        String requestUrl = ((FilterInvocation) o).getRequestUrl();
//        //查询具体某个接口的权限
//        List<Permission> permissionList =  sysPermissionService.selectListByPath(requestUrl);
//        if(permissionList == null || permissionList.size() == 0){
//            //请求路径没有配置权限，表明该请求接口可以任意访问
//            return null;
//        }
//        String[] attributes = new String[permissionList.size()];
//        for(int i = 0;i<permissionList.size();i++){
//            attributes[i] = permissionList.get(i).getPermissionCode();
//        }
//        List<String> list=new ArrayList<>();
//        openFeignEducationRolePermission.rpGetAll();
//        openFeignEducationPermission.getAll();
//        通过URL获得可以访问该URL的ROLEList表
        List<String> list = openFeignEducationAPI.selectRoleEntitiesByPermissionURL(requestUrl);
        if (list==null||list.size()==0){
            //请求路径没有配置权限，表明该请求接口可以任意访问
            return null;
        }
        String[] strings = new String[list.size()];
       for (int i=0;i<list.size();i++){
           strings[i]=list.get(i);
       }
          return SecurityConfig.createList(strings);

    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}

